using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectLoaiSach()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAISACH
			};
			return View(CreateViewModel(data));
		}



		public ActionResult DeleteLoaiSach(int id, int page = 1)
		{
			var result = _repLoaiSach.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_LOAISACH,
				CurrentPage = page
			};
			return View("SelectLoaiSach", CreateViewModel(data));
		}

		public ActionResult EditLoaiSach(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAISACH,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditLoaiSach", CreateViewModel(data));
		}

		public ActionResult CancelEditingLoaiSach(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAISACH,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLoaiSach", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateLoaiSach(FormCollection forms)
		{
			string id = forms.GetValues("LoaiSach_ID")[0];
			string tenloaisach = forms.GetValues("LoaiSach_TenLoaiSach")[0];
			var loaisach = _repLoaiSach.SelectByID(int.Parse(id));
			loaisach.TenLoaiSach = tenloaisach;
			var result = _repLoaiSach.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAISACH,
			};
			return View("SelectLoaiSach", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertLoaiSach(FormCollection forms)
		{
			string tenloaisach = forms.GetValues("LoaiSach_TenLoaiSach")[0];
			var newItem = new LoaiSach
			{
				TenLoaiSach = tenloaisach
			};
			var result = _repLoaiSach.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAISACH,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectLoaiSach", CreateViewModel(data));
		}


	}
}